package com.warm.androidstudy.utils;

import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;

import androidx.core.app.NotificationCompat;


import com.warm.androidstudy.R;

import static android.app.Notification.VISIBILITY_SECRET;
import static androidx.core.app.NotificationCompat.PRIORITY_HIGH;

/**
 * Created by LRJ on 2019/12/20.
 * Describe :通知栏工具类
 */
public class NotificationUtil extends ContextWrapper {
    public static final String CHANNEL_ID = "default";
    private static final String CHANNEL_NAME = "聊天消息";
    private NotificationManager mManager;
    private Context mContext;

    public NotificationUtil(Context context) {
        super(context);
        mContext = context;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            createNotificationChannel();
        }
    }

    @TargetApi(Build.VERSION_CODES.O)
    private void createNotificationChannel() {
        NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
//        channel.setShowBadge(false);//是否显示角标
        //是否绕过请勿打扰模式
        channel.canBypassDnd();
        //闪光灯
        channel.enableLights(true);
        //锁屏显示通知
        channel.setLockscreenVisibility(VISIBILITY_SECRET);
        //闪关灯的灯光颜色
        channel.setLightColor(Color.GREEN);
        //桌面launcher的消息角标
        channel.canShowBadge();
        //是否允许震动
        channel.enableVibration(true);
        //获取系统通知响铃声音的配置
        channel.getAudioAttributes();
        //获取通知取到组
        channel.getGroup();
        //设置可绕过  请勿打扰模式
        channel.setBypassDnd(true);
        //设置震动模式
        channel.setVibrationPattern(new long[]{100, 100, 200});
        //是否会有灯光
        channel.shouldShowLights();
        getManager().createNotificationChannel(channel);

    }

    private NotificationManager getManager() {
        if (mManager == null) {
            mManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        }
        return mManager;
    }

    /**
     * 创建通知
     *
     * @param title
     * @param content
     * @return
     */
    private NotificationCompat.Builder getNotification(String title, String content) {
        NotificationCompat.Builder builder = null;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            builder = new NotificationCompat.Builder(getApplicationContext(), CHANNEL_ID);
        } else {
            builder = new NotificationCompat.Builder(getApplicationContext());
            builder.setPriority(PRIORITY_HIGH);
        }
//        builder.setTicker("测试");
        //标题
        builder.setContentTitle(title);
        //文本内容
        builder.setContentText(content);
        //文字颜色
        builder.setColor(Color.parseColor("#005dac"));
        //小图标，Icon 如果不设置Icon,Notification不会跳出来
        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), R.mipmap.ic_launcher));
        //设置点击信息后自动清除通知,true-点击后自动清除,false-点击以后不会自动清除
        builder.setAutoCancel(true);
        //设置声音、震动
        builder.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE);
        //通知的时间
        builder.setWhen(System.currentTimeMillis());
        return builder;
    }

    /**
     * 获取带有进度的Notification
     *
     * @param title   标题
     * @param content 内容
     * @param intent  跳转意图
     * @return
     */
    private NotificationCompat.Builder getNotificationWithPendingIntent(String title, String content, PendingIntent intent) {
        NotificationCompat.Builder builder = null;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            builder = new NotificationCompat.Builder(getApplicationContext(), CHANNEL_ID);
        } else {
            builder = new NotificationCompat.Builder(getApplicationContext());
            builder.setPriority(PRIORITY_HIGH);
        }
        builder.setTicker("测试");
        //标题
        builder.setContentTitle(title);
        //文本内容
        builder.setContentText(content);
        //文字颜色
        builder.setColor(Color.parseColor("#005dac"));
        //小图标，Icon 如果不设置Icon,Notification不会跳出来
        builder.setSmallIcon(R.mipmap.ic_launcher);
        //设置大图标，未设置时使用小图标代替，拉下通知栏显示的那个图标
        //设置大图片 BitmpFactory.decodeResource(Resource res,int id) 根据给定的资源Id解析成位图
        builder.setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), R.mipmap.ic_launcher));
        //设置点击信息后自动清除通知,true-点击后自动清除,false-点击以后不会自动清除
        builder.setAutoCancel(true);
        //设置声音、震动
        builder.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE);
        //通知的时间
        builder.setWhen(System.currentTimeMillis());
        //设置点击信息后的跳转（意图）
        builder.setContentIntent(intent);
        return builder;
    }

    /**
     * 获取带有进度的Notification
     *
     * @param title    标题
     * @param content  内容
     * @param progress 进度
     * @param intent   跳转意图
     * @return
     */
    private NotificationCompat.Builder getNotificationProgress(String title, String content,
                                                               int progress, PendingIntent intent) {
        NotificationCompat.Builder builder = null;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            builder = new NotificationCompat.Builder(getApplicationContext(), CHANNEL_ID);
        } else {
            builder = new NotificationCompat.Builder(getApplicationContext());
            builder.setPriority(PRIORITY_HIGH);
        }
        builder.setTicker("测试");
        //标题
        builder.setContentTitle(title);
        //文本内容
        builder.setContentText(content);
        //文字颜色
        builder.setColor(Color.parseColor("#005dac"));
        //小图标，Icon 如果不设置Icon,Notification不会跳出来
        builder.setSmallIcon(R.mipmap.ic_launcher);
        //设置大图标，未设置时使用小图标代替，拉下通知栏显示的那个图标
        //设置大图片 BitmpFactory.decodeResource(Resource res,int id) 根据给定的资源Id解析成位图
        builder.setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), R.mipmap.ic_launcher));
        //设置点击信息后自动清除通知,true-点击后自动清除,false-点击以后不会自动清除
        builder.setAutoCancel(true);
        //设置声音、震动
        builder.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE);
        //通知的时间
        builder.setWhen(System.currentTimeMillis());
        //设置点击信息后的跳转（意图）
        builder.setContentIntent(intent);

        if (progress > 0 && progress < 100) {
            //一种是有进度刻度的（false）,一种是循环流动的（true）
            //设置为false，表示刻度，设置为true，表示流动
            builder.setProgress(100, progress, false);
        } else {
            //0,0,false,可以将进度条隐藏
            builder.setProgress(0, 0, false);
            builder.setContentText("下载完成");
        }
        return builder;
    }

    //TODO *****************************************发送通知**************************************************

    /**
     * 发送通知（通知id固定为1）
     *
     * @param title   标题
     * @param content 内容
     */
    public void sendNotification(String title, String content) {
        NotificationCompat.Builder builder = getNotification(title, content);
        Notification notification = builder.build();
        notification.flags = Notification.FLAG_AUTO_CANCEL;
        getManager().notify(1, notification);
    }

    public void sendNotification(String title, String content, PendingIntent contextIntent) {
        NotificationCompat.Builder builder = getNotificationWithPendingIntent(title, content, contextIntent);
        builder.setContentIntent(contextIntent);
        Notification notification = builder.build();
        notification.flags = Notification.FLAG_AUTO_CANCEL;
        getManager().notify(1, notification);
    }

    /**
     * 发送通知
     *
     * @param notifyId 通知分类id
     * @param title    标题
     * @param content  内容
     */
    public void sendNotification(int notifyId, String title, String content) {
        NotificationCompat.Builder builder = getNotification(title, content);
        Notification notification = builder.build();
        notification.flags = Notification.FLAG_AUTO_CANCEL;
        getManager().notify(notifyId, notification);
    }

    /**
     * 发送通知
     *
     * @param notifyId      通知分类id
     * @param title         标题
     * @param content       内容
     * @param contextIntent 跳转意图
     */
    public void sendNotification(int notifyId, String title, String content, PendingIntent contextIntent) {
        NotificationCompat.Builder builder = getNotificationWithPendingIntent(title, content, contextIntent);
        builder.setContentIntent(contextIntent);
        Notification notification = builder.build();
        notification.flags = Notification.FLAG_AUTO_CANCEL;
        getManager().notify(notifyId, notification);
    }

//TODO ******************************设置开启通知栏****************************************************

    /**
     * 开启通知栏通知
     */
    public void ifNotificationunEnabledOpen() {

    }

    public  void toSetting() {
        Intent intent = new Intent();
        intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
        Uri uri = Uri.fromParts("package", mContext.getPackageName(), null);
        intent.setData(uri);
        mContext.startActivity(intent);
    }
}
